<?php
include "./inc/header.php";

title('字符串方法');

clogList(
	array(
		'strtoupper',
		'strtolower',
		'---',
		'addslashes',
		'chr',
		'ord',
		'---',
		'substr',
		'strpos',
		'strlen',
		'strstr',
		'str_replace',
		'---',
		'explode',
		'implode',
		'---',
		'print',
		'print_r',
		'echo'
	)
);

comment('大小写转换');
clog(strtoupper('hello too'));

comment('反斜杠转义');
clog(addslashes('hel/lo-www\os'));

comment('获取code point对应的字符');
$sCodePoint = ord('s');
$eCodePoint = ord('e');
clog(chr($sCodePoint));
clog(chr($sCodePoint) . chr($eCodePoint) . chr($eCodePoint));

$string1 = "I need cookies & soda.";
$needle = "cookies";

comment('获取子字符串substr');
//find length of the needle
$needle_len = strlen($needle);
//find postion
$position_num = strpos($string1, $needle) + $needle_len;
//cut the string
$result_string = substr($string1, 0, $position_num);
//display it
clog($result_string); // I need cookies

comment('获取子字符串strstr');
$email = 'name@example.com';
$domain = strstr($email, '@');
clog($domain); // 打印 @example.com
$user = strstr($email, '@', true);
clog($user); // 打印 name

comment('字符串替换');
clog(str_replace('.com', '', 'hello-world.com'));

comment('字符串分割为数组');
print_r(explode(',', 'one,two,three'));

comment('数组聚合成字符串');
clog(implode(' ', array('how', 'are', 'you', 'today?')));

include "inc/footer.php";